define(["angularAMD"], function (angularAMD) {
    angularAMD.service('$MaterialSetUp', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            var service = this;

            function sub_release_part_info_get(dialog){
                var content={ //物料子資訊
                    wo_no:dialog.wo_no,
                    release_part_no:dialog.materialItem.group_material_no,
                    return_type:'0', //發料單號
                    op_no: dialog.item.op_no
                };

                $MMWService.sendToServer({
                    uri:'ServicesSTD.Module_MAT.sub_release_part_info_get',
                    content : content,
                    success : function(data){
                        dialog.materialsInfo = data.sub_release_part_info_get;
                        dialog.materialsInfo_back = data.sub_release_part_info_get.slice();
                    }
                });
            }

            function SetMaterialsToMaterialsProcess(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_MAT.set_materials_to_materials_process',
                    content : content,
                    success : function(data){
                        $rootScope.showToast($filter('translate')('mobile01.materialsetup.msg.success'));
                        if(callback)
                            callback();
                    }
                });
            }

            service.init = function(options){
                (function openDialog(){
                    $mdDialog.dialog(
                        'module/moduleMobile/program/mobile01/MaterialSetUp.tmp.html',
                        function (dialog) {
                            return {
                                item:options.item,
                                materialItem:{
                                    group_material_no:'',
                                    materials_lot_no:'',
                                    qty:''
                                },
                                materialsInfo:[],
                                materialsInfo_back:[],
                                back:function(){
                                    dialog.hide();
                                },
                                loadMaterialNoList: function () { //查詢發料單號
                                    // 20230314 modify by Alan for#130236 : 替換page_mode為form_name
                                    $MMWService.showQueryListPNIG({
                                        form_name: 'PageNo_52',
                                        condition: [
                                            {name:$filter('translate')('mobile01.materialsetup.material_no'), column_name:'MaterialInNo', query_mode:'2', value:dialog.materialItem.group_material_no}
                                        ],
                                        header: [
                                            {label:$filter('translate')('mobile01.materialsetup.material_no'), code:'page_no'}
                                        ],
                                        setCondition:function(conditions){
                                            if(dialog.wo_no != '')
                                                conditions.push(
                                                    {column_name:'A.MONo', query_mode:'0', value: dialog.item.wo_no, merge_condition_model: 'AND'},
                                                    {column_name:'C.OpNo', query_mode:'0', value: dialog.item.op_no, merge_condition_model: 'AND'}
                                                );
                                            return conditions;
                                        },
                                        confirm : function(selectItem){
                                            dialog.materialItem.group_material_no = selectItem.page_no;
                                            //組出物料清單
                                            sub_release_part_info_get(dialog);
                                        }
                                    });
                                },
                                //add by Oliver for M#72191 上料測試錯誤
                                add_or_loadPatchNo:function(){
                                    if(dialog.materialItem.materials_lot_no === ''){
                                        dialog.loadPatchNoList();
                                    }else{
                                        dialog.add();
                                    }
                                },
                                loadPatchNoList:function(){
                                    if(dialog.materialItem.group_material_no !=''){
                                        $rootScope.showQueryList({
                                            key:'materials_lot_no',
                                            header : [
                                                {label: $filter('translate')('mobile01.materialsetup.materials_no'), code:'materials_no'},
                                                {label: $filter('translate')('mobile01.materialsetup.patch_no'), code:'materials_lot_no'}
                                            ],
                                            query: function(conditions, feedback){
                                                feedback(dialog.materialsInfo_back);
                                            },
                                            confirm: function(selectItem, radioDialog){
                                                dialog.materialItem.materials_no = selectItem.materials_no;
                                                dialog.materialItem.materials_lot_no = selectItem.materials_lot_no;
                                                dialog.materialItem.qty = selectItem.qty;
                                                radioDialog.hide();
                                            }
                                        });
                                    } else {
                                        $rootScope.showAlert($filter('translate')('mobile01.materialsetup.msg.group_material_no_empty'));
                                    }
                                },
                                add:function(){
                                    if(dialog.materialItem.materials_lot_no ==''){
                                        $rootScope.showAlert($filter('translate')('mobile01.materialsetup.msg.materials_lot_no_empty'));
                                    } else if(dialog.materialItem.qty =='' || dialog.materialItem.qty <= 0){
                                        $rootScope.showAlert($filter('translate')('mobile01.materialsetup.msg.qty_is_zero'));
                                    } else {
                                        var temp = $filter('filter')(dialog.materialsInfo,
                                            {materials_lot_no:dialog.materialItem.materials_lot_no, materials_no:dialog.materialItem.material_no});
                                        if(temp && temp.length>0){
                                            $rootScope.showAlert($filter('translate')('mobile01.materialsetup.msg.materials_lot_no_double'));
                                        } else {
                                            dialog.materialsInfo.push(dialog.materialItem);
                                        }
                                    }
                                },
                                remove:function(item){
                                    dialog.materialsInfo.splice(dialog.materialsInfo.indexOf(item), 1);
                                },
                                confirm: function () {
                                    var isZero = false;
                                    for(var i=0; i<dialog.materialsInfo.length; i++){
                                        dialog.materialsInfo[i].wo_no=dialog.item.wo_no;
                                        if(dialog.materialsInfo[i].qty == 0){
                                            isZero = true;
                                        }
                                    }
                                    if(!isZero){
                                        var content = {
                                            report_datetime:new Date(),
                                            machine_no:options.machine_no,
                                            materials_detail:dialog.materialsInfo,
                                            op_no:options.item.op_no //add by Oliver for M#72191 上料測試錯誤
                                        }
                                        SetMaterialsToMaterialsProcess(content, function(){
                                            dialog.hide();
                                            if(options.callback)
                                                options.callback();
                                        });
                                    } else {
                                        $rootScope.showAlert($filter('translate')('mobile01.materialsetup.msg.qty_is_zero'));
                                    }
                                }
                            }
                        }
                    );
                })();
            };
        }
    ]);
});
